package com.ruoyi.mall.app.service.impl;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.mall.app.repository.DtsCategoryEsRepository;
import com.ruoyi.mall.app.repository.DtsGoodsEsRepository;
import com.ruoyi.mall.app.service.DtsGoodsEsService;
import com.ruoyi.mall.common.domain.DtsCategory;
import com.ruoyi.mall.common.domain.DtsCategoryEs;
import com.ruoyi.mall.common.domain.DtsGoods;
import com.ruoyi.mall.common.domain.DtsGoodsEs;
import com.ruoyi.mall.common.mapper.DtsCategoryMapper;
import com.ruoyi.mall.common.mapper.DtsGoodsApiMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.TimerTask;
import java.util.stream.Collectors;

@Service
public class DtsGoodsEsServiceImpl implements DtsGoodsEsService {

    private final DtsGoodsApiMapper goodsApiMapper;
    private final DtsGoodsEsRepository goodsEsRepository;
    private final DtsCategoryEsRepository categoryEsRepository;

    private final DtsCategoryMapper categoryMapper;

    @PostConstruct
    public void init() {
//        syncAllToElasticsearch();
        selectCategoryGoodsEs();
    }

    public DtsGoodsEsServiceImpl(DtsGoodsApiMapper goodsApiMapper, DtsGoodsEsRepository goodsEsRepository, DtsCategoryEsRepository categoryEsRepository, DtsCategoryMapper categoryMapper) {
        this.goodsApiMapper = goodsApiMapper;
        this.goodsEsRepository = goodsEsRepository;
        this.categoryEsRepository = categoryEsRepository;
        this.categoryMapper = categoryMapper;
    }

    @Override
    public void syncAllToElasticsearch() {
        List<DtsGoods> goodsList = goodsApiMapper.selectGoodsByDtsGoods(new DtsGoods(), 0);
        List<DtsGoodsEs> esList = goodsList.stream().map(this::convertToEs).collect(Collectors.toList());
        goodsEsRepository.saveAll(esList);
    }

    @Override
    public void selectCategoryGoodsEs() {
        List<DtsCategory> categoryList = categoryMapper.selectDtsCategoryList(new DtsCategory());
        List<DtsCategoryEs> esList = categoryList.stream().map(this::categoryEs).collect(Collectors.toList());
        categoryEsRepository.saveAll(esList);
    }


    @Override
    public List<DtsGoodsEs> searchGoodsEs(String keyword) {
        return goodsEsRepository.findByNameContaining(keyword);
    }

    private DtsGoodsEs convertToEs(DtsGoods goods) {
        DtsGoodsEs es = new DtsGoodsEs();
        BeanUtils.copyProperties(goods, es);
        es.setId(goods.getId().toString());
        es.setAddTime(goods.getAddTime());
        es.setRetailPrice(goods.getRetailPrice().doubleValue());
        es.setCounterPrice(goods.getCounterPrice().doubleValue());


        es.setApproveMsg(goods.getApproveMsg());
        es.setApproveStatus((byte) goods.getApproveStatus().intValue());
        es.setBrokerageType((byte) goods.getBrokerageType().intValue());
        es.setCommpany(goods.getCommpany());
        es.setNew(goods.getIsNew());
        es.setHot(goods.getIsHot());
        es.setOnSale(goods.getIsOnSale());
        es.setShareUrl(goods.getShareUrl());

        return es;
    }

    private DtsCategoryEs categoryEs(DtsCategory dtsCategory) {
        DtsCategoryEs es = new DtsCategoryEs();
        BeanUtils.copyProperties(dtsCategory, es);
        es.setId(dtsCategory.getId().toString());
        es.setName(dtsCategory.getName());
        es.setKeywords(dtsCategory.getKeywords());
        es.setDesc(dtsCategory.getDesc());
        es.setPid(Math.toIntExact(dtsCategory.getPid()));
        es.setIconUrl(dtsCategory.getIconUrl());
        es.setPicUrl(dtsCategory.getPicUrl());
        es.setLevel(dtsCategory.getLevel());
        es.setSortOrder(dtsCategory.getSortOrder());
        es.setAddTime(dtsCategory.getAddTime());
        es.setUpdateTime(dtsCategory.getUpdateTime());
        es.setDeleted(dtsCategory.getDeleted());
        return es;
    }
}
